{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 10 Minutes to pandas"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请参阅[官方文档](http://pandas.pydata.org/pandas-docs/stable/10min.html)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 设置为 inline 风格\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 包导入\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 创建数据集对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     1\n",
       "1     3\n",
       "2     5\n",
       "3   NaN\n",
       "4     6\n",
       "5     8\n",
       "dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Series 对象可以理解为一维数组\n",
    "s = pd.Series([1, 3, 5, np.nan, 6, 8])\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-01', '2016-03-02', '2016-03-03', '2016-03-04',\n",
       "               '2016-03-05', '2016-03-06'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# DataFrame 对象可以理解为二维数组，可以指定索引格式\n",
    "dates = pd.date_range('20160301', periods=6)\n",
    "dates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.188983</td>\n",
       "      <td>-1.150119</td>\n",
       "      <td>-0.700588</td>\n",
       "      <td>0.439065</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>-2.041544</td>\n",
       "      <td>1.084507</td>\n",
       "      <td>-0.335441</td>\n",
       "      <td>1.969754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>1.204151</td>\n",
       "      <td>-1.277714</td>\n",
       "      <td>-0.230671</td>\n",
       "      <td>0.629063</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-0.352351</td>\n",
       "      <td>-1.701585</td>\n",
       "      <td>-0.034294</td>\n",
       "      <td>-0.330139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>0.627601</td>\n",
       "      <td>-0.292939</td>\n",
       "      <td>0.457975</td>\n",
       "      <td>2.262402</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-1.121869</td>\n",
       "      <td>-0.533223</td>\n",
       "      <td>0.627452</td>\n",
       "      <td>0.412665</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-01  1.188983 -1.150119 -0.700588  0.439065\n",
       "2016-03-02 -2.041544  1.084507 -0.335441  1.969754\n",
       "2016-03-03  1.204151 -1.277714 -0.230671  0.629063\n",
       "2016-03-04 -0.352351 -1.701585 -0.034294 -0.330139\n",
       "2016-03-05  0.627601 -0.292939  0.457975  2.262402\n",
       "2016-03-06 -1.121869 -0.533223  0.627452  0.412665"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=list('ABCD'))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.18898298, -1.15011854, -0.70058776,  0.43906549],\n",
       "       [-2.04154443,  1.08450747, -0.33544069,  1.96975377],\n",
       "       [ 1.2041512 , -1.27771421, -0.23067059,  0.62906316],\n",
       "       [-0.35235094, -1.70158492, -0.03429361, -0.33013878],\n",
       "       [ 0.62760104, -0.29293918,  0.45797463,  2.26240237],\n",
       "       [-1.12186945, -0.53322343,  0.6274522 ,  0.41266481]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "      <th>F</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2016-03-01</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>text</td>\n",
       "      <td>AA</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2016-03-01</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>text</td>\n",
       "      <td>BB</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>2016-03-01</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>text</td>\n",
       "      <td>CC</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>2016-03-01</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>text</td>\n",
       "      <td>DD</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   A          B  C  D     E   F\n",
       "0  1 2016-03-01  0  5  text  AA\n",
       "1  1 2016-03-01  1  6  text  BB\n",
       "2  1 2016-03-01  2  7  text  CC\n",
       "3  1 2016-03-01  3  8  text  DD"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用字典来创建：key 为 DataFrame 的列；value 为对应列下的值\n",
    "df = pd.DataFrame({\n",
    "                  'A': 1,\n",
    "                  'B': pd.Timestamp('20160301'),\n",
    "                  'C': range(4),\n",
    "                  'D': np.arange(5, 9),\n",
    "                  'E': 'text',\n",
    "                  'F': ['AA', 'BB', 'CC', 'DD']})\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A             int64\n",
       "B    datetime64[ns]\n",
       "C             int64\n",
       "D             int64\n",
       "E            object\n",
       "F            object\n",
       "dtype: object"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    1\n",
       "2    1\n",
       "3    1\n",
       "Name: A, dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(df.A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查看数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-01  1.313419  0.826457 -1.574146  0.525008\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建数据集\n",
    "n_rows = 6\n",
    "dates = pd.date_range('20160301', periods=n_rows)\n",
    "df = pd.DataFrame(np.random.randn(n_rows, 4), index=dates, columns=list('ABCD'))\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(6, 4)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-01  1.313419  0.826457 -1.574146  0.525008\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-01  1.313419  0.826457 -1.574146  0.525008\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "DatetimeIndex(['2016-03-01', '2016-03-02', '2016-03-03', '2016-03-04',\n",
       "               '2016-03-05', '2016-03-06'],\n",
       "              dtype='datetime64[ns]', freq='D')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([u'A', u'B', u'C', u'D'], dtype='object')"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.31341924,  0.82645709, -1.57414606,  0.52500758],\n",
       "       [ 0.02839742, -1.00934929,  0.32701362,  0.91824786],\n",
       "       [-0.85700833, -1.68269525,  0.646229  , -0.18337746],\n",
       "       [-1.11288513, -1.49166212, -1.11482404, -0.11561882],\n",
       "       [-0.44871305, -0.16365107, -1.23029491,  1.10665563],\n",
       "       [-0.26786722,  0.09231292, -0.48023763, -0.80992272]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>6.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>-0.224110</td>\n",
       "      <td>-0.571431</td>\n",
       "      <td>-0.571043</td>\n",
       "      <td>0.240165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.856808</td>\n",
       "      <td>0.983304</td>\n",
       "      <td>0.898112</td>\n",
       "      <td>0.734900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>-0.809923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>-0.754935</td>\n",
       "      <td>-1.371084</td>\n",
       "      <td>-1.201427</td>\n",
       "      <td>-0.166438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>-0.358290</td>\n",
       "      <td>-0.586500</td>\n",
       "      <td>-0.797531</td>\n",
       "      <td>0.204694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>-0.045669</td>\n",
       "      <td>0.028322</td>\n",
       "      <td>0.125201</td>\n",
       "      <td>0.819938</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>1.106656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              A         B         C         D\n",
       "count  6.000000  6.000000  6.000000  6.000000\n",
       "mean  -0.224110 -0.571431 -0.571043  0.240165\n",
       "std    0.856808  0.983304  0.898112  0.734900\n",
       "min   -1.112885 -1.682695 -1.574146 -0.809923\n",
       "25%   -0.754935 -1.371084 -1.201427 -0.166438\n",
       "50%   -0.358290 -0.586500 -0.797531  0.204694\n",
       "75%   -0.045669  0.028322  0.125201  0.819938\n",
       "max    1.313419  0.826457  0.646229  1.106656"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2016-03-01 00:00:00</th>\n",
       "      <th>2016-03-02 00:00:00</th>\n",
       "      <th>2016-03-03 00:00:00</th>\n",
       "      <th>2016-03-04 00:00:00</th>\n",
       "      <th>2016-03-05 00:00:00</th>\n",
       "      <th>2016-03-06 00:00:00</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.028397</td>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.267867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>0.092313</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>-0.480238</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>0.525008</td>\n",
       "      <td>0.918248</td>\n",
       "      <td>-0.183377</td>\n",
       "      <td>-0.115619</td>\n",
       "      <td>1.106656</td>\n",
       "      <td>-0.809923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   2016-03-01  2016-03-02  2016-03-03  2016-03-04  2016-03-05  2016-03-06\n",
       "A    1.313419    0.028397   -0.857008   -1.112885   -0.448713   -0.267867\n",
       "B    0.826457   -1.009349   -1.682695   -1.491662   -0.163651    0.092313\n",
       "C   -1.574146    0.327014    0.646229   -1.114824   -1.230295   -0.480238\n",
       "D    0.525008    0.918248   -0.183377   -0.115619    1.106656   -0.809923"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 6)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.T.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>D</th>\n",
       "      <th>C</th>\n",
       "      <th>B</th>\n",
       "      <th>A</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>0.525008</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>1.313419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.918248</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.028397</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.183377</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>-0.857008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-0.115619</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.112885</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>1.106656</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-0.448713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.809923</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.267867</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   D         C         B         A\n",
       "2016-03-01  0.525008 -1.574146  0.826457  1.313419\n",
       "2016-03-02  0.918248  0.327014 -1.009349  0.028397\n",
       "2016-03-03 -0.183377  0.646229 -1.682695 -0.857008\n",
       "2016-03-04 -0.115619 -1.114824 -1.491662 -1.112885\n",
       "2016-03-05  1.106656 -1.230295 -0.163651 -0.448713\n",
       "2016-03-06 -0.809923 -0.480238  0.092313 -0.267867"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_index(axis=1, ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-01  1.313419  0.826457 -1.574146  0.525008\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.sort_values(by='C')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-03-01    1.313419\n",
       "2016-03-02    0.028397\n",
       "2016-03-03   -0.857008\n",
       "2016-03-04   -1.112885\n",
       "2016-03-05   -0.448713\n",
       "2016-03-06   -0.267867\n",
       "Freq: D, Name: A, dtype: float64"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['20160302':'20160305']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 通过标签选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1.313419\n",
       "B    0.826457\n",
       "C   -1.574146\n",
       "D    0.525008\n",
       "Name: 2016-03-01 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['20160301']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(df.loc['20160301'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2016-03-01  1.313419  0.826457\n",
       "2016-03-02  0.028397 -1.009349\n",
       "2016-03-03 -0.857008 -1.682695\n",
       "2016-03-04 -1.112885 -1.491662\n",
       "2016-03-05 -0.448713 -0.163651\n",
       "2016-03-06 -0.267867  0.092313"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[:, ['A', 'B']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2016-03-01  1.313419  0.826457\n",
       "2016-03-02  0.028397 -1.009349\n",
       "2016-03-03 -0.857008 -1.682695\n",
       "2016-03-04 -1.112885 -1.491662\n",
       "2016-03-05 -0.448713 -0.163651"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['20160301':'20160305', ['A', 'B']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.3134192362700037"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc['2016-03-01', 'A']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.3134192362700037"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.at[pd.Timestamp('2016-03-01'), 'A']\n",
    "# df.at['2016-03-01', 'A'] will raise error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 通过位置选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.028397\n",
       "B   -1.009349\n",
       "C    0.327014\n",
       "D    0.918248\n",
       "Name: 2016-03-02 00:00:00, dtype: float64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B\n",
       "2016-03-03 -0.857008 -1.682695\n",
       "2016-03-04 -1.112885 -1.491662\n",
       "2016-03-05 -0.448713 -0.163651"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[2:5, 0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[1:5, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.009349292057921"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iloc[1, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.009349292057921"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iat[1, 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 布尔索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.A < 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.525008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1.106656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>NaN</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D\n",
       "2016-03-01  1.313419  0.826457       NaN  0.525008\n",
       "2016-03-02  0.028397       NaN  0.327014  0.918248\n",
       "2016-03-03       NaN       NaN  0.646229       NaN\n",
       "2016-03-04       NaN       NaN       NaN       NaN\n",
       "2016-03-05       NaN       NaN       NaN  1.106656\n",
       "2016-03-06       NaN  0.092313       NaN       NaN"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df > 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "df['tag'] = ['a'] * 2 + ['b'] * 2 + ['c'] * 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>tag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "      <td>c</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "      <td>c</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D tag\n",
       "2016-03-01  1.313419  0.826457 -1.574146  0.525008   a\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248   a\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377   b\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619   b\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656   c\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923   c"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>tag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "      <td>c</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "      <td>c</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D tag\n",
       "2016-03-01  1.313419  0.826457 -1.574146  0.525008   a\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248   a\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656   c\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923   c"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.tag.isin(['a', 'c'])]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 修改数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>tag</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "      <td>a</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "      <td>b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "      <td>c</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "      <td>c</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D tag\n",
       "2016-03-01  1.313419  0.826457 -1.574146  0.525008   a\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248   a\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377   b\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619   b\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656   c\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923   c"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2016-03-01    0\n",
       "2016-03-02    1\n",
       "2016-03-03    2\n",
       "2016-03-04    3\n",
       "2016-03-05    4\n",
       "2016-03-06    5\n",
       "Freq: D, dtype: int64"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(np.arange(6), index=pd.date_range('20160301', periods=6))\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df['E'] = s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>tag</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>1.313419</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "      <td>b</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "      <td>b</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "      <td>c</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "      <td>c</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D tag  E\n",
       "2016-03-01  1.313419  0.826457 -1.574146  0.525008   a  0\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248   a  1\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377   b  2\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619   b  3\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656   c  4\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923   c  5"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df.loc['20160301', 'A'] = 0.2\n",
    "# df.['20160301', 'A'] = 0.2 will not have effect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>tag</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "      <td>b</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "      <td>b</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "      <td>c</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "      <td>c</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D tag  E\n",
       "2016-03-01  0.200000  0.826457 -1.574146  0.525008   a  0\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248   a  1\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377   b  2\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619   b  3\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656   c  4\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923   c  5"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "df.at[pd.Timestamp('20160301'), 'A'] = 0.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>tag</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>0.400000</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "      <td>b</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "      <td>b</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "      <td>c</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "      <td>c</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D tag  E\n",
       "2016-03-01  0.400000  0.826457 -1.574146  0.525008   a  0\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248   a  1\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377   b  2\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619   b  3\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656   c  4\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923   c  5"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>tag</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>0.600000</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>0.028397</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-0.857008</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "      <td>b</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.112885</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "      <td>b</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.448713</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "      <td>c</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.267867</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "      <td>c</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   A         B         C         D tag  E\n",
       "2016-03-01  0.600000  0.826457 -1.574146  0.525008   a  0\n",
       "2016-03-02  0.028397 -1.009349  0.327014  0.918248   a  1\n",
       "2016-03-03 -0.857008 -1.682695  0.646229 -0.183377   b  2\n",
       "2016-03-04 -1.112885 -1.491662 -1.114824 -0.115619   b  3\n",
       "2016-03-05 -0.448713 -0.163651 -1.230295  1.106656   c  4\n",
       "2016-03-06 -0.267867  0.092313 -0.480238 -0.809923   c  5"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.iat[0, 0] = 0.6\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>tag</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>10</td>\n",
       "      <td>0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "      <td>0.525008</td>\n",
       "      <td>a</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>11</td>\n",
       "      <td>-1.009349</td>\n",
       "      <td>0.327014</td>\n",
       "      <td>0.918248</td>\n",
       "      <td>a</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>12</td>\n",
       "      <td>-1.682695</td>\n",
       "      <td>0.646229</td>\n",
       "      <td>-0.183377</td>\n",
       "      <td>b</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>13</td>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "      <td>-0.115619</td>\n",
       "      <td>b</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>14</td>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "      <td>1.106656</td>\n",
       "      <td>c</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>15</td>\n",
       "      <td>0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "      <td>-0.809923</td>\n",
       "      <td>c</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             A         B         C         D tag  E\n",
       "2016-03-01  10  0.826457 -1.574146  0.525008   a  0\n",
       "2016-03-02  11 -1.009349  0.327014  0.918248   a  1\n",
       "2016-03-03  12 -1.682695  0.646229 -0.183377   b  2\n",
       "2016-03-04  13 -1.491662 -1.114824 -0.115619   b  3\n",
       "2016-03-05  14 -0.163651 -1.230295  1.106656   c  4\n",
       "2016-03-06  15  0.092313 -0.480238 -0.809923   c  5"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[:, 'A'] = np.arange(10, 16)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2016-03-01</th>\n",
       "      <td>-0.826457</td>\n",
       "      <td>-1.574146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-02</th>\n",
       "      <td>-1.009349</td>\n",
       "      <td>-0.327014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-03</th>\n",
       "      <td>-1.682695</td>\n",
       "      <td>-0.646229</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-04</th>\n",
       "      <td>-1.491662</td>\n",
       "      <td>-1.114824</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-05</th>\n",
       "      <td>-0.163651</td>\n",
       "      <td>-1.230295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-03-06</th>\n",
       "      <td>-0.092313</td>\n",
       "      <td>-0.480238</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   B         C\n",
       "2016-03-01 -0.826457 -1.574146\n",
       "2016-03-02 -1.009349 -0.327014\n",
       "2016-03-03 -1.682695 -0.646229\n",
       "2016-03-04 -1.491662 -1.114824\n",
       "2016-03-05 -0.163651 -1.230295\n",
       "2016-03-06 -0.092313 -0.480238"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2 = df.loc[:, ['B', 'C']].copy()\n",
    "df2[df2 > 0] = -df2\n",
    "df2"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
